package com.moranyunxi.www;

import java.util.*;
import java.util.stream.Collectors;

public class CollectionOperateTest {
    public static void main(String[] args) {
//        test1();
        test2();
    }

    private static void test1() {
        List<Integer> list = new ArrayList<>();
        list.add(100);
        list.add(900);
        list.add(500);
        list.add(54);
        list.add(967);

        System.out.println(list);
        List<Integer> collect = list.stream().sorted(Integer::compareTo).collect(Collectors.toList());
        System.out.println(collect);
    }

    private static void test2() {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> resutlMap1 = new HashMap<>();
        resutlMap1.put("index", 251);
        resutlMap1.put("src", "第一个视频");
        list.add(resutlMap1);

        Map<String, Object> resutlMap2 = new HashMap<>();
        resutlMap2.put("index", 56);
        resutlMap2.put("src", "第er个视频");
        list.add(resutlMap2);

        Map<String, Object> resutlMap3 = new HashMap<>();
        resutlMap3.put("index", 336);
        resutlMap3.put("src", "第3个视频");
        list.add(resutlMap3);

        System.out.println(list);
        List<Map<String, Object>> collect = list.stream().sorted(Comparator.comparing(CollectionOperateTest::getMapIndex))
                .collect(Collectors.toList());
        System.out.println(collect);
    }

    private static Integer getMapIndex(Map<String, Object> map) {
        return (Integer) map.get("index");
    }
}
